package com.mall.service.homepage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mall.commons.common.CommonPage;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.SmsHomeRecommendProduct;
import com.mall.model.domain.SmsHomeRecommendSubject;
import com.mall.service.homepage.service.HomeRecommendProductService;
import com.mall.service.homepage.service.HomeRecommendSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/home/recommendSubject")
public class HomeRecommendSubjectController {
    @Autowired
    private HomeRecommendSubjectService homeRecommendSubjectService;


    /**
     * 添加首页新品
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody List<SmsHomeRecommendSubject> smsHomeRecommendSubjects){
        homeRecommendSubjectService.saveBatch(smsHomeRecommendSubjects);
        return CommonResult.success("添加成功");
    }
    /**
     * 批量删除首页新品
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestParam List<Long> ids){
        homeRecommendSubjectService.removeByIds(ids);
        return CommonResult.success("删除成功");
    }
    /**
     * 分页查询首页新品
     */
    @GetMapping("/list")
    public CommonResult<CommonPage<SmsHomeRecommendSubject>> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                  @RequestParam(defaultValue = "10") Integer pageSize, String subjectName, Integer recommendStatus){
        CommonPage<SmsHomeRecommendSubject> page =homeRecommendSubjectService.findByPage(pageNum,pageSize,subjectName,recommendStatus);

        return CommonResult.success(page);
    }
    /**
     * 批量修改推荐品牌状态
     */
    @PostMapping("/update/recommendStatus")
    public CommonResult updateRecommendStatus(@RequestParam List<Long> ids , Integer recommendStatus){
        LambdaQueryWrapper<SmsHomeRecommendSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SmsHomeRecommendSubject::getId,ids);
        List<SmsHomeRecommendSubject> list = homeRecommendSubjectService.list(queryWrapper);
        ArrayList<SmsHomeRecommendSubject> objects = new ArrayList<>();
        for (SmsHomeRecommendSubject smsHomeRecommendSubject : list) {

            smsHomeRecommendSubject.setRecommendStatus(recommendStatus);
            objects.add(smsHomeRecommendSubject);
        }

        homeRecommendSubjectService.updateBatchById(objects);
        return CommonResult.success("批量修改成功");
    }
    /**
     * 修改推荐品牌排序
     */
    @PostMapping("/update/sort/{id}")
    public CommonResult updateSort(@PathVariable Long id,Integer sort){
        SmsHomeRecommendSubject recommendProduct = new SmsHomeRecommendSubject();
        recommendProduct.setId(id);
        recommendProduct.setSort(sort);
        homeRecommendSubjectService.updateById(recommendProduct);
        return CommonResult.success("修改成功");
    }
}
